package com.smartcruise.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;

import com.smartcruise.util.customView.LoadingDialog;

import io.realm.Realm;

/**
 * Created by FengChaoQun
 * on 2018/1/19 0019
 */

public class BaseFragment extends Fragment implements BaseNormalView {
    protected BaseActivity baseActivity;
    protected View rootView;
    private boolean isDefaultPopFragment = true;//标记是否默认右滑关闭fragment
    private BaseActivity.RightSlide rightSlide;
    protected Realm realm;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseActivity = (BaseActivity) getActivity();
        setDefaultRightSlide();
        realm = Realm.getDefaultInstance();
    }

    @Override
    public void onResume() {
        super.onResume();
        setRightSlide(rightSlide);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            setRightSlide(rightSlide);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        realm.close();
    }

    @Override
    public void showToast(CharSequence msg) {
        baseActivity.showToast(msg);
    }

    @Override
    public void showToast(int msg) {
        baseActivity.showToast(msg);
    }

    @Override
    public void showLoadingDialog(String msg) {
        baseActivity.showLoadingDialog(msg);
    }

    @Override
    public void setLoadingDialogText(String text) {
        baseActivity.setLoadingDialogText(text);
    }

    @Override
    public void hideLoadingDialog() {
        baseActivity.hideLoadingDialog();
    }

    @Override
    public BaseNormalView getBaseView() {
        return this;
    }

    @Override
    public Realm getRealm() {
        return realm;
    }

    protected View getRootView(int rootViewId) {
        rootView = View.inflate(baseActivity, rootViewId, null);
        return rootView;
    }

    /**
     * *describe:设置右滑关闭当前activity
     */
    public void setCloseActivity() {
        clearRightSlide();
        setRightSlide(new BaseActivity.RightSlide() {
            @Override
            public boolean rightSlide() {
                baseActivity.finish();
                return true;
            }
        });
    }

    private void setDefaultRightSlide() {
        setRightSlide(new BaseActivity.RightSlide() {
            @Override
            public boolean rightSlide() {
                getFragmentManager().popBackStack();
                return true;
            }
        });
    }

    public void setRightSlide(BaseActivity.RightSlide rightSlide) {
        this.rightSlide = rightSlide;
        baseActivity.setRightSlide(rightSlide);
    }

    @Override
    public void clearRightSlide() {
        rightSlide = null;
        baseActivity.clearRightSlide();
    }

    @Override
    public void setEnableRightSlide(boolean enable) {
        if (!enable) {
            clearRightSlide();
        }
        baseActivity.setEnableRightSlide(enable);
    }

    @Override
    public void setOnLoadingDialogDismiss(LoadingDialog.onDismiss on) {
        baseActivity.setOnLoadingDialogDismiss(on);
    }

    public boolean isDefaultPopFragment() {
        return isDefaultPopFragment;
    }

    public void setDefaultPopFragment(boolean defaultPopFragment) {
        isDefaultPopFragment = defaultPopFragment;
    }
}
